const { getHtml } = require('./utils/http')
const database = require('./datebase')
const url = 'https://m.anjuke.com/nj/xiaoqu'
const log =require('./utils/log')
let res = {
    count: 0,
    update: 0,
    add: 0,
    isFinished: false
}
database.connect({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'spider'
})
let delay = 5000 //每次请求间隔
const areas = []
/**
 * 获取区域列表
 * @param {String} cityUrl 城市地址
 */
const getAreas = async (cityUrl) => {
    let $ = await getHtml(cityUrl, delay)
    let pages = $('.list_filter .listcont a')
    if (pages.length === 0) {
        log.i('未获取到区域列表，爬虫结束。')
        return false
    }
    for (let i = 0; i < pages.length; i++) {
        let item = $(pages[i])
        if (item.text() !== '全部') {
            let findRes = areas.find((area) => {area.name === item.text()})
            if (!findRes) {
                areas.push({name:item.text(), url: item.attr('href'), isFinished: false})
            }
        }
    }
    for (let key in areas) {
        let area = areas[key]
        if (area.isFinished) {
            continue
        }
        let res = await getVillages(area.url, area.name)
        if (!res) {
           log.i('未发现小区列表，爬虫停止:' + area.url)
           return false
        }
        areas[key].isFinished = true
    }
    return true
}
/**
 * 打开区域列表，获取小区名称列表
 * @param {String} url 区域地址
 * @param {String} name 区域名字
 */
const getVillages = async (url, name ) => {
    // 获取第一页
    let $ = await getHtml(url, delay)
    if (!$){
        return false;
    }
    let res = await saveVillages($, name)
    if (!res) {
        return false
    }
    // 解析其他页
    let pages = []
    $('.seo_pages a').each((i, elem) => {
        if (i !== 0) {
            pages.push($(elem).attr('href'))
        }
    })
    let total = pages.length
    for (let i = 0; i < total; i++) {
        let $ = await getHtml(pages[i], delay)
        let res = await saveVillages($,name)
        if (!res) {
            return false
        }   
    }
    return true
}
/**
 * 
 * @param {*} $
 * @param {*} url 当前网址
 * @param {*} name 区域名称
 */
const saveVillages = async ($,name) => {
    let pages =  $('.mod-bd .itemsCont .items a')
    if (pages.length === 0) {
        return false
    }
    for (let i = 0; i < pages.length; i++){
        let page = $(pages[i])
        let params = { name: page.text(), url: page.attr('href'), area: name }
        let exists = await database.exists('village', {name: params.name, area: params.area}) // 区域和小区名称
        if (!exists) {
            let id = await database.insert('village', params)
            log.i('成功保存小区：' + params.name)
            res.add++
        } else {
            log.i('成功更新小区：' + params.name)
            res.update++
        }
        res.count++
    }
    return true
}
const getVillageDetails = async () => {
    let res = await database.query('select * from village where status <> 1 limit 0, 100')
    
    if (res.length > 0) {

    } else {
        log.i('全部小区更新完成')
        return true
    }
    for (let item of res) {
        let $ = await getHtml(item.url, 10000)
        if (!$) {
            log.i('网络错误，跳过')
            continue
        }
        if ($('#verify_page').length > 0) {
            log.i('出现验证，停止。')
            return false
        }
        await saveDetail($, item)
    }
    return await getVillageDetails()
}
const saveDetail = async ($, item) => {
    let village = {}
    village.update_at = new Date()
    village.id = item.id
    village.status = 1
    village.price = $('.main-item .txt-c .price').text()
    let infos = $('.header-field span')
    village.wuye_type = $(infos[0]).text().trim().replace('物业类型：', '')
    village.wuye_price = $(infos[1]).text().trim().replace('物业费：', '')
    village.finished_at = $(infos[2]).text().trim().replace('竣工时间：', '')
    village.green = $(infos[3]).text().trim().replace('绿化率：', '')
    village.hushu = $(infos[4]).text().trim().split('：')[1],
    village.rongjilv = $(infos[5]).text().trim().replace('容积率：', '')
    let other = $('.comm-other-field dd')
    village.developers = $(other[0]).text()
    village.wuye = $(other[1]).text()
    let location = $('.comm-map img').data('center')
    village.lng = location.split(',')[0]
    village.lat = location.split(',')[1]
    await database.update('village', village)
    log.i('成功更新:'+ item.name)
    return true
}
const getList = async () => {
    res.isFinished = await getAreas(url)
    if (res.isFinished) {
        database.connection.destroy()
    } else {
        log.i('爬虫停止，50秒后重新启动')
        setTimeout(getList, 50000)
    }
    log.i(`本次运行结果：${res.count}，更新：${res.update}，增加：${res.add}`)

}
const updateDetailList = async () => {
    let res = await getVillageDetails()
    if (!res) {
        log.i('爬虫停止，50秒后重新启动')
        setTimeout(updateDetailList, 50000)
    }
}
const start = async () => {
    updateDetailList()
}
start()